/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.nya.DAO;

import desu.db.HibernateUtil;
import desu.nya.Studio;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
/**
 *
 * @author Fess
 */
public class StudioIMPL implements StudioDAO{
    
    @Override
    public void addStudio(Studio studio) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(studio);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при вставке студии");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }    
        
    }
    @Override
    public Studio getStudioByID(int id) throws SQLException {
        Session session=null;
        Studio studio=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            studio=(Studio)session.load(Studio.class, id);
        }
        catch(Exception ex){
            System.err.println("Ошибка при получении студии");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }    
        return studio;
    }
    @Override
    public void updateStudio(int studio_id, Studio studio) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(studio);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при обновлении студии");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        } 
    }
    @Override
    public List<Studio> getAllStudio() throws SQLException {
        Session session=null;
        List<Studio> list=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            list=session.createCriteria(Studio.class).addOrder(Order.asc("studioName")) .list();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        } 
        return list;
    }
    @Override
    public void deleteStudio(Studio studio) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(studio);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при вставке студии");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }  
    }

    @Override
    public Studio getStudioByIdFromWA(int idWA) throws SQLException {
        Session session=null;
        Studio studio=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
//            studio=(Studio)session.load(Studio.class, id);
            List list=session.createCriteria(Studio.class).add(Expression.eq("idWa", idWA)).list();
            if(!list.isEmpty())studio=(Studio)list.get(0);
        }
        catch(Exception ex){
            System.err.println("Ошибка при получении студии");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }    
        return studio;
    }
    
}
